package com.ssm.blog.controller;

import com.ssm.blog.pojo.AdminUser;
import com.ssm.blog.pojo.Menu;
import com.ssm.blog.pojo.TreeNode;
import com.ssm.blog.service.MenuService;
import com.ssm.blog.util.Constants;
import com.ssm.blog.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.security.auth.Subject;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/menu.html")
public class MenuController {

    @Autowired
    private MenuService menuService;

    //去菜单列表页
    @RequestMapping(params = "act=list")
    public ModelAndView list() {
        return new ModelAndView("menu_list");
    }

    //加载菜单列表
    @RequestMapping(params = "act=tree")
    @ResponseBody
    public List<TreeNode> getAllMenu()
            throws Exception {
        //加载左边的菜单，要从session中获取用用户
        return menuService.getAllMenu();
    }

    // 编辑菜单
    @RequestMapping(params = "act=edit")
    @ResponseBody
    public Map<String, Object> add(Menu menu) {
        if (menu.getId() == null) {//如果没有id就添加
            menuService.addMenu(menu);
        } else {
            menuService.updateMenu(menu);
        }
        return JsonUtil.getSuccessMessage("编辑成功", null);
    }

    // 删除菜单
    @RequestMapping(params = "act=delete")
    @ResponseBody
    public Map<String, Object> delete(Integer[] ids) {
        menuService.deleteMenu(ids);
        return JsonUtil.getSuccessMessage("删除成功", null);
    }

}
